package com.example.test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import net.duohuo.dhroid.callback.BeanCallBack;
import net.duohuo.dhroid.net.OkGo;
import net.duohuo.dhroid.net.cache.CacheMode;
import net.duohuo.dhroid.net.model.DResponse;
import net.duohuo.dhroid.net.request.GetRequest;
import net.duohuo.dhroid.net.request.PostRequest;
import net.duohuo.dhroid.util.MapUtil;
import net.duohuo.dhroid.util.PhotoUtil;
import okhttp3.Call;
import okhttp3.Response;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.example.test.api.API;
import com.example.test.api.Constant;
import com.example.test.views.SelectPicturePop;
import com.example.test.views.SelectPicturePop.OnStateChangeListener;

public class MainActivity extends FragmentActivity {

	File mCacheDir;

	String mPhotoPath;

	ImageView picI;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mCacheDir = new File(getExternalCacheDir(), "Xiaoqu");
		mCacheDir.mkdirs();
		picI = (ImageView) findViewById(R.id.pic);
		findViewById(R.id.post).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				login();
			}
		});

		findViewById(R.id.get).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				getUserinfo();
			}
		});

		findViewById(R.id.post_file).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				selectPic();
			}
		});

		findViewById(R.id.list).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent it = new Intent(MainActivity.this, ListActivity.class);
				startActivity(it);

			}
		});

		findViewById(R.id.gridview).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent it = new Intent(MainActivity.this,
						GridViewActivity.class);
				startActivity(it);
			}
		});

		findViewById(R.id.recyclerview).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent it = new Intent(MainActivity.this,
								RecyclerViewActivity.class);
						startActivity(it);
					}
				});
	}

	private void login() {
		PostRequest request = OkGo.post(new API().login);
		request.tag(this);
		request.params("pswd", "123456c");
		request.params("phone", "18502519390");
		request.params("communityid", "1");

		request.execute(new BeanCallBack<HashMap<String, Object>>(
				MainActivity.this) {

			@Override
			public void onsuccessUI(
					DResponse<HashMap<String, Object>> response, Call call) {
				
				System.out.println("数据" +response.jSONFrom("data"));

			}
		}.setFormWhat("data"));

	}

	private void getUserinfo() {
		GetRequest request = OkGo.get(new API().userInfo);
		request.cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST);
		request.tag(this);
		request.params("communityid", "1");
		request.params("user_id", "7");

		request.execute(new BeanCallBack<HashMap<String, Object>>(
				MainActivity.this) {

			@Override
			public void onsuccessUI(
					DResponse<HashMap<String, Object>> response, Call call) {

				if (response.isSuccess()) {
					JSONObject jo = response.jSONFrom("data");
					String result = response.result;

					Map<String, Object> map = (Map<String, Object>) response.data
							.get("data");

					System.out.println("map" + map);

				}

			}
		}.setFormWhat("data"));
	}

	private void selectPic() {
		SelectPicturePop pop = SelectPicturePop.getInstance(MainActivity.this);
		pop.setOnStateChangeListener(new OnStateChangeListener() {

			@Override
			public void close(String photoPath) {
				mPhotoPath = photoPath;
			}
		});
		pop.show(mCacheDir);
	}

	private void postFile(String path) {
		PostRequest request = OkGo.post(new API().uploadImg);
		request.tag(this);
		request.params("communityid", "1");
		request.params("user_id", "7");
		request.params("image", new File(path));
		request.execute(new BeanCallBack<HashMap<String, Object>>(
				MainActivity.this, "图片上传中...") {

			@Override
			public void onsuccessUI(
					DResponse<HashMap<String, Object>> response, Call call) {

				if (response.isSuccess()) {

				}
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {

			case Constant.PICK_PHOTO:
				String newpicPath = new File(mCacheDir,
						System.currentTimeMillis() + ".jpg").getAbsolutePath();
				String path1 = PhotoUtil.onPhotoFromPick(MainActivity.this,
						Constant.ZOOM_PIC, mPhotoPath, data, 2, 1, 1000,
						newpicPath);
				mPhotoPath = path1;
				break;
			case Constant.ZOOM_PIC:
				picI.setScaleType(ScaleType.FIT_XY);
				picI.setImageBitmap(PhotoUtil
						.getLocalImage(new File(mPhotoPath)));
				postFile(mPhotoPath);
				break;

			case Constant.TAKE_PHOTO:

				String newtakePath = new File(mCacheDir,
						System.currentTimeMillis() + ".jpg").getAbsolutePath();
				String path = PhotoUtil.onPhotoFromCamera(MainActivity.this,
						Constant.ZOOM_PIC, mPhotoPath, 2, 1, 1000, newtakePath);
				mPhotoPath = path;
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
